﻿// (c) Copyright Fernando Cerqueira.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using EnvDTE;
using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Shell;
using AppRes = WindowsPhoneCommands.Resources;


// ReSharper disable CheckNamespace
namespace WindowsPhoneCommands
// ReSharper restore CheckNamespace
{
    /// <summary>
    /// Interaction logic for XapDeploy.xaml
    /// </summary>
    // ReSharper disable RedundantExtendsListEntry
    public partial class TargetFolderDialog : DialogWindow
    // ReSharper restore RedundantExtendsListEntry
    {
        public Project SelectedProject;
        public ToolWindowPane ParentWindow;
        public string[] SelectFolder;
        public string NeutralFile;
        private bool _loaded;
        private string _rootProject;
        private List<string> _previewFiles;

        public WindowsPhoneCommandsPackage Package
        {
            get
            {
                return (WindowsPhoneCommandsPackage)ParentWindow.Package;
            }
        }

        public TargetFolderDialog()
        {
            InitializeComponent();
            Background = VsTheming.GetDialogBackground();
            Foreground = VsTheming.GetWindowText();
            BntOk.IsEnabled = false;
            _previewFiles = new List<string>();
            Loaded += TargetFolderDialogLoaded;
        }

        private void TargetFolderDialogLoaded(object sender, RoutedEventArgs e)
        {
            if (!_loaded)
            {
                Title = AppRes.ToolWindowTitle + " " + AppRes.XTitDialogTargetFolder;
                CboFolder.ItemsSource = EnvPhoneDevice.GetListFolderFromProject(SelectedProject);
                var directoryInfo = new FileInfo(SelectedProject.FullName).Directory;
                if (directoryInfo != null)
                {
                    _rootProject = directoryInfo.FullName + "\\";
                }
            }
            _loaded = true;
        }

        private void CboFolderSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            // ReSharper disable PossibleNullReferenceException
            var defres = _rootProject + (string)e.AddedItems[0];
            // ReSharper restore PossibleNullReferenceException
            var fi = new FileInfo(defres + "\\" + NeutralFile);
            var fname = fi.FullName.Replace(fi.Extension, "");

            var keys = EnvPhoneDevice.PhoneLanguageSupport().Keys.ToArray();
            var lngs = new List<string>();
            _previewFiles = new List<string>();
            foreach (var key in keys)
            {
                var lngname = fname + "." + key + fi.Extension;
                var item = Package.EnvDte.Solution.FindProjectItem(lngname);
                if (item == null)
                {
                    lngname = fname + "." + key.Substring(0, 2) + fi.Extension;
                    item = Package.EnvDte.Solution.FindProjectItem(lngname);
                }
                if (item != null)
                {
                    _previewFiles.Add(lngname);
                    lngs.Add(item.Name);
             
                }
            }
            LstFiles.ItemsSource = null;
            LstFiles.ItemsSource = lngs.ToArray();
            BntOk.IsEnabled = (lngs.Count > 0);
        }

        private void BntOkClick(object sender, RoutedEventArgs e)
        {
            SelectFolder = _previewFiles.ToArray();
            Close();
        }

    }
}
